﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Firearm.cs" company="">
//   
// </copyright>
// <summary>
// A weapon that the player use to shoot a projectile far to him.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

/*
 * Author: Barbara Carboni
 * Created: 17-11-2011
 * Revised: 04-12-2011
 */

namespace ITU_vs_Zombies.Stuff
{
    using System;
    using System.Diagnostics.Contracts;

    using ITU_vs_Zombies.Game;
    using ITU_vs_Zombies.Movement;
    using ITU_vs_Zombies.Scenery;

    using Microsoft.Xna.Framework;

    /// <summary>
    /// A weapon that the player use to shoot a projectile far to him.
    /// </summary>
    [Serializable]
    public class Firearm : Weapon
    {
        #region Constants and Fields

        /// <summary>
        /// The Image of the projectile.
        /// </summary>
        private string imageP;

        /// <summary>
        /// The number of frames of the projectile.
        /// </summary>
        private uint numberFramesP;

        /// <summary>
        /// The speed of the projectile.
        /// </summary>
        private int speedP;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="Firearm"/> class.
        /// </summary>
        /// <param name="image">
        /// The Image. 
        /// </param>
        /// <param name="numberFrames">
        /// The number of frames. 
        /// </param>
        /// <param name="numberFloor">
        /// The number of floor. 
        /// </param>
        /// <param name="position">
        /// The position. 
        /// </param>
        /// <param name="damage">
        /// The damage. 
        /// </param>
        /// <param name="chargeRate">
        /// The charge Rate.
        /// </param>
        /// <param name="imageP">
        /// The Image of the projectile.
        /// </param>
        /// <param name="numberFramesP">
        /// The number Frames of the projectile.
        /// </param>
        /// <param name="speedP">
        /// The speed of the projectile.
        /// </param>
        /// <param name="sound">
        /// The sound of the projectile.
        /// </param>
        public Firearm(
            string image, 
            uint numberFrames, 
            int numberFloor, 
            Vector2 position, 
            uint damage, 
            TimeSpan chargeRate, 
            string imageP, 
            uint numberFramesP, 
            int speedP, 
            string sound)
            : base(image, numberFrames, numberFloor, position, damage, chargeRate, sound)
        {
            Contract.Requires(image != null);
            Contract.Requires(imageP != null);
            Contract.Requires(sound != null);
            Contract.Requires(numberFrames > 0);
            Contract.Requires(numberFramesP > 0);
            Contract.Requires(damage > 0);
            Contract.Requires(position.X >= 0 && position.Y >= 0);
            Contract.Requires(speedP > 0);
            this.imageP = imageP;
            this.numberFramesP = numberFramesP;
            this.speedP = speedP;
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// The clone.
        /// </summary>
        /// <returns>
        /// The clone.
        /// </returns>
        public override object Clone()
        {
            var firearm = (Firearm)base.Clone();
            firearm.imageP = this.imageP;
            firearm.numberFramesP = this.numberFramesP;
            firearm.speedP = this.speedP;
            return firearm;
        }

        /// <summary>
        /// The hit.
        /// </summary>
        /// <param name="gameTime">
        /// The game Time.
        /// </param>
        /// <param name="position">
        /// The position.
        /// </param>
        /// <param name="direction">
        /// The direction.
        /// </param>
        public override void Use(GameTime gameTime, Vector2 position, MovementsEnum direction)
        {
            if (!this.CanUse(gameTime))
            {
                return;
            }

            this.Sound.Play();
            this.LastUse = gameTime.TotalGameTime;
            Floor floor = Client.Floors.Find(f => f.Number == this.NumberFloor);
            floor.AddExtraObject(
                new Projectile(
                    this.imageP, 
                    this.numberFramesP, 
                    this.NumberFloor, 
                    position, 
                    this.Damage, 
                    direction, 
                    this.speedP, 
                    @"Sounds\Credits"));
        }

        #endregion

        #region Methods

        /// <summary>
        /// The can use.
        /// </summary>
        /// <param name="gameTime">
        /// The game time.
        /// </param>
        /// <returns>
        /// The can use.
        /// </returns>
        protected override bool CanUse(GameTime gameTime)
        {
            return (gameTime.TotalGameTime - this.LastUse).TotalMilliseconds >= this.ChargeRate.TotalMilliseconds;
        }

        #endregion
    }
}